package com.iamk.util;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import weka.core.Instances;

public class GetData {
	public static double[][] getData(Instances instances, int label) {
		double[][] data = getDataMatrix(instances);
		int row = 0;
		for (int i = 0; i < data.length; i++) {
			if(data[i][data[0].length-1] == label){
				row++;
			}
		}
		double[][] subData = new double[row][data[0].length];
		int idxRow = 0;
		for (int i = 0; i < data.length; i++) {
			if(data[i][data[0].length-1] == label){
				for(int j = 0; j < data[0].length && idxRow < row; j++){
					subData[idxRow][j] = data[i][j];
				}
				idxRow++;
			}
		}
		return subData;
	}

	public static double[][] getDataMatrix(Instances mInstances) {
		double[][] data = null;
		int row = mInstances.numInstances();
		int col = mInstances.numAttributes();
		data = new double[row][col];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
//				System.out.println(Double.valueOf(mInstances.get(i).toString(j)));
				data[i][j] =Double.valueOf(mInstances.get(i).toString(j));
			}
//			System.out.print(data[i][9] + " ");
		}
		return data;
	}

	public static double[][] getSubMatrix(double[][] parent, int m) {
		double[][] parentData = parent.clone();
		double[][] sub = null;
		if (m <= parentData[0].length) {
			sub = new double[parentData.length][m];
			for (int i = 0; i < parentData.length; i++) {
				for (int j = 0; j < m; j++) {
					sub[i][j] = parentData[i][j];
				}
			}
		}
		return sub;
	}
	
	/**
	 * Method get row of parent
	 * @param double[][] parent matrix
	 * @param int n row index*/
	public static double[] getRowMatrix(double[][] parent, int n) {
		double[][] parentData = parent.clone();
		double[] row = null;
		if (n <= parent.length) {
			row = new double[parentData[0].length];
			for (int j = 0; j < parentData[0].length; j++) {
				row[j] = parentData[n][j];
			}
		}
		return row;
	}
	
	public static double[][] getSubMatrixWithLabel(double[][] parent, int m) {
		double[][] parentData = parent.clone();
		double[][] sub = null;
		if (m <= parentData[0].length) {
			sub = new double[parentData.length][m+1];
			for (int i = 0; i < parentData.length; i++) {
				for (int j = 0; j < m; j++) {
					sub[i][j] = parentData[i][j];
				}
				sub[i][m]=parentData[i][parentData[0].length-1];
			}
		}
		return sub;
	}
	
	public static double[][] getSubMatrix(double[][] parent, int n, int m, int idx) {
		double[][] parentData = parent.clone();
		double[][] sub = null;
		if (m <= parentData[0].length && n < parentData.length && idx < parentData.length) {
			sub = new double[n][m];
			int numRow = n+idx;
			int idxSub = 0;
			for (int i = idx; i < numRow; i++) {
				for (int j = 0; j < m; j++) {
					sub[idxSub][j] = parentData[i][j];
				}
				idxSub++;
			}
		}
		return sub;
	}
	
	public static double[][] getSubMatrixInvert(double[][] parent,int n,int m,int idx){
//		System.out.println("Parent size: " + parent.length);
//		System.out.println("n: " + n + " idx: " + idx );
		
		double[][] parentData = parent.clone();
		double[][] sub = null;
		if (m <= parentData[0].length && n < parentData.length && idx < parentData.length) {
			sub = new double[parent.length-n][m];
			for (int i = 0; i < idx; i++) {
				for (int j = 0; j < m; j++) {
					sub[i][j] = parentData[i][j];
				}
			}
			
			int idxSub = idx;
			for (int i = n + idx; i < parent.length; i++) {
				for (int j = 0; j < m; j++) {
					sub[idxSub][j] = parentData[i][j];
				}
				idxSub++;
			}
		}
		return sub;
	}
	
	public static int[][] readMaskFile(String path) {
		int[][] mask = null;
		int rows = 0;
		BufferedReader br = null;
		try {
			// Open the file that is the first
			FileReader fstream = new FileReader(path);
			br = new BufferedReader(fstream);

			String strLine = null;
			List<String> stringRow = new ArrayList<String>();
			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				// Add number from file to list
				stringRow.add(strLine);
				rows++;
			}
			// Close the input stream
			mask = new int[rows][stringRow.get(0).split(",").length];
			for(int i = 0; i < stringRow.size(); i++){
				String[] arrNums = stringRow.get(i).split(",");
				for(int j = 0; j < arrNums.length; j++){
					mask[i][j] = Integer.parseInt(arrNums[j]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (br != null)br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return mask;
	}

	public static BufferedImage getRegion(BufferedImage img, int[][] mask,
			int region) {
		BufferedImage imgRegion = img;
		System.out.println("Im size: " + img.getWidth() + "/" + img.getHeight());
		System.out.println("Mask size: " + mask.length + "/" + mask[0].length);
		for (int i = 0; i < mask.length; i++) {
			for (int j = 0; j < mask[0].length; j++) {
				if (mask[i][j] == region) {
					imgRegion.setRGB(i, j, imgRegion.getRGB(i, j) & 0xF00);
				}
			}
		}
		return imgRegion;
	}

	public static BufferedImage copyImage(BufferedImage source) {
		BufferedImage b = new BufferedImage(source.getWidth(),
				source.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics g = b.getGraphics();
		g.drawImage(source, 0, 0, null);
		g.dispose();
		return b;
	}

	public static BufferedImage toBufferedImage(Image img) {
		if (img instanceof BufferedImage) {
			return (BufferedImage) img;
		}

		// Create a buffered image with transparency
		BufferedImage bimage = new BufferedImage(img.getWidth(null),
				img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

		// Draw the image on to the buffered image
		Graphics2D bGr = bimage.createGraphics();
		bGr.drawImage(img, 0, 0, null);
		bGr.dispose();

		// Return the buffered image
		return bimage;
	}
	
}
